package com.example.miwalkvediorecv;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class VedioRecv implements SurfaceHolder.Callback{
	public static final String tag = "VedioRecv";
	SurfaceView surfaceView;
	SurfaceHolder holder;
	ServerSocket serverSocket = null;
	Socket recvSocket = null;
	boolean isRuning = false;
	int ServerPort;
	
	public VedioRecv(SurfaceView sur, int port){
		surfaceView = sur;
		ServerPort = port;
		Log.i(tag,"addcallback");
		
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		Log.i(tag, "OnsurfaceCreated!");
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void initHolder(){
		holder = surfaceView.getHolder();
		holder.addCallback(this);
	}
	
	public void startRecv(){
		initHolder();
		new socketThread().start();
	}
	
	
	private class socketThread extends Thread{
		static final String tag = "socketThread";
		InputStream socketIns = null;
		Canvas canvas = null;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				serverSocket = new ServerSocket(ServerPort);
				Log.i(tag, "create a serverSocket");
				recvSocket = serverSocket.accept();
				if(recvSocket==null){
					Log.e(tag, "recvSocket get failed!");
					return;
				}
				else
					Log.i(tag, "recvSocket get!");
				socketIns = recvSocket.getInputStream();
				if(socketIns==null){
					Log.e(tag,"socketIns is NULL");
					return;
				}
				else {
					Log.e(tag,"socketIns got!");
				}
					
				
				while(true){
					BitmapFactory.Options opt = new BitmapFactory.Options();
					opt.inPreferredConfig = Bitmap.Config.RGB_565;
					opt.inPurgeable = true;
					opt.inInputShareable = true;
					Bitmap  bitmap = BitmapFactory.decodeStream(socketIns,null,opt);
					if(bitmap==null){
						Log.e(tag,"bitmap is null");
						continue;
					}
					else {
						Log.i(tag,"bitmap got!");
					}
					Matrix m = new Matrix();
					m.setRotate(90, bitmap.getWidth()/2,bitmap.getHeight()/2);
					canvas = holder.lockCanvas(null);
					canvas.drawBitmap(bitmap, m, null);
					holder.unlockCanvasAndPost(canvas);
					Log.i(tag,"draw a bitmap");
					
					if(bitmap != null && !bitmap.isRecycled()){
						bitmap.recycle();
						System.gc();
						bitmap = null;
					}
				}
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
}

